package live.jialing.sip.base.service;

import com.google.common.collect.Maps;
import live.jialing.core.data.LinkType;
import live.jialing.core.data.jpa.CustomJpaRepository;
import live.jialing.core.service.BaseDomainService;
import live.jialing.core.service.GeneralService;
import live.jialing.data.util.SpecificationUtil;
import live.jialing.sip.base.domain.Child;
import live.jialing.sip.base.domain.Parent;
import live.jialing.sip.base.repository.ParentRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 家长
 */
@Service
public class ParentService extends BaseDomainService<Parent, Long> implements GeneralService<Parent, Long> {

    private static Logger logger = LoggerFactory.getLogger(ParentService.class);

    @Resource
    private ParentRepository parentRepository;

    @Resource
    private ChildService childService;


    @Override
    public CustomJpaRepository getRepository() {
        return this.parentRepository;
    }

    public Specification<Parent> buildSpecification(Class<Parent> classz, Map<String, Object> searchParams, LinkType type) {

        return SpecificationUtil.buildSpecification(classz, searchParams, type);
    }

    /**
     * 新增家长并关联孩子
     *
     * @param childId 孩子id
     * @param parent  家长信息
     */
    public void addAndRelationChild(long childId, Parent parent) {

        if (parent == null || childId < 1) {
            logger.warn("参数为null或不正确：parent isNull={}，childId={}", parent == null, childId);
            return;
        }

        Child child = childService.findByPK(childId);
        if (child == null) {
            logger.warn("孩子id={}不正确，系统无此id的孩子", childId);
            return;
        }

        parent.getChildren().add(child);
        this.add(parent);
    }

    /**
     * 家长关联孩子
     *
     * @param childId  孩子id
     * @param parentId 家长id
     */
    public void relationChild(long childId, long parentId) {

        Child child = childService.findByPK(childId);
        if (child == null) {
            logger.warn("孩子id={}不正确，系统无此id的孩子", childId);
            return;
        }

        Parent parent = parentRepository.findParentFetchChildByPK(parentId);
        if (parent == null) {
            logger.warn("家长id={}不正确，系统无此id的家长", parentId);
            return;
        }

        parent.getChildren().add(child);
        this.edit(parent);
    }

    /**
     * 家长取消关联孩子
     *
     * @param childId  孩子id
     * @param parentId 家长id
     */
    public void unRelationChild(long childId, long parentId) {

        Parent parent = parentRepository.findParentFetchChildByPK(parentId);
        if (parent == null) {
            logger.warn("家长id={}不正确，系统无此id的家长", parentId);
            return;
        }

        Optional<Child> isc = parent.getChildren().stream().filter(c -> Long.compare(c.getId().longValue(), childId) == 0).findFirst();
        if (!isc.isPresent()) {
            logger.warn("孩子id={}不正确，系统无此id的孩子", childId);
        }

        parent.getChildren().remove(isc.get());
        this.edit(parent);
    }

    /**
     * 根据手机号查询家长
     *
     * @param mobile
     * @return
     */
    public List<Parent> findByMobile(String mobile) {

        Map<String, Object> searchParams = Maps.newTreeMap();
        searchParams.put("EQ_mobile", mobile);
        return this.findAll(searchParams, null);
    }
}
